package cn.youwan.market.message;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.youwan.market.entity.AppListBean;
import cn.youwan.market.entity.PromotionListbean;
import cn.youwan.market.entity.PromotionListbean.Appbean;
import cn.youwan.market.http.StarHttpClient;
import cn.youwan.market.utils.Const;
import cn.youwan.market.utils.Utils;

import android.content.Context;
import android.util.Log;


public class YouwanMessage {
	private Context context;

	public YouwanMessage(Context context) {
		this.context = context;
	}

	/*// 注册
	public NameTestBean UserReg(String data) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("data", data);
				hpc.sendPost(Const.Host + Const.reg, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				NameTestBean parser = new NameTestBean(message);
				// LogInBean parser = new LogInBean(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 登录
	public NameTestBean UserLogin(String data) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("data", data);
				hpc.sendPost(Const.Host + Const.login, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				NameTestBean parser = new NameTestBean(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 找回密码
	public boolean findPass(String userName) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("username", userName);
				hpc.sendPost(Const.Host + Const.findPass,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				boolean b = false;
				try {
					JSONObject j = new JSONObject(message);
					b = j.optString("result").equals("success");
				} catch (Exception e) {
					e.printStackTrace();
				}
				return b;
			} else {
				return false;
			}
		} catch (IOException e) {
			return false;
		} catch (Exception e) {
			return false;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 推荐页
	public PromotionListbean getPromotionList(boolean b) {
		StarHttpClient hpc = new StarHttpClient();
		hpc.setKey(Const.PromotionList);
		hpc.setIsRefresh(b);
		try {
			hpc.sendPost(Const.Host + Const.PromotionList,
					Utils.isCMWAP(context));
			String message = hpc.getReciveData();
			PromotionListbean parser = new PromotionListbean(message);
			return parser;
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 游戏中的精品
	public PromotionListbean getGameCompetitive() {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.sendPost(Const.Host + Const.GameCompetitive,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				PromotionListbean parser = new PromotionListbean(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 应用中的精品
	public PromotionListbean getAppCompetitive() {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.sendPost(Const.Host + Const.AppCompetitive,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				PromotionListbean parser = new PromotionListbean(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 获取专题
	public SpecailBean getSpecails(String id) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("topic_id", id);
				hpc.sendPost(Const.Host + Const.Specali, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				SpecailBean parser = new SpecailBean(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 获取app详情
	public Appbean getAppDetail(String id) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("id", id);
				hpc.sendPost(Const.Host + Const.AppDetail,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				Appbean parser = new Appbean(new JSONObject(message));
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	// 获取游戏排行
	public AppListBean getAppsList(String url, int page) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				//hpc.addParam("page", String.valueOf(page));
				//hpc.sendPost(Const.Host + url, Utils.isCMWAP(context));
				hpc.sendPost(url, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				Log.i("downs2", "2:"+message);
				AppListBean parser = new AppListBean(new JSONArray(message));
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}

	/*// 获取游戏分类
	public AppCatoryBean getGameCatory(String url) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.sendPost(Const.Host + url, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				AppCatoryBean parser = new AppCatoryBean(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	// 获取分类下列表
//	public AppListBean getCatoryById(String url, String id, int page) {
//		StarHttpClient hpc = new StarHttpClient();
//		try {
//			if (Utils.hasNetwork(this.context)) {
//				hpc.addParam("class_id", id);
//				hpc.addParam("page", String.valueOf(page));
//				hpc.sendPost(Const.Host + url, Utils.isCMWAP(context));
//				String message = hpc.getReciveData();
//				AppListBean parser = new AppListBean(new JSONObject(message));
//				return parser;
//			} else {
//				return null;
//			}
//		} catch (IOException e) {
//			return null;
//		} catch (Exception e) {
//			return null;
//		} finally {
//			hpc.dispose();
//		}
//	}

	/*// 搜索
	public AppListBean search(String keyWord) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				// String ss = URLEncoder.encode(keyWord, "UTF-8");
				String ss = keyWord;
				hpc.addParam("keyword", ss);
				hpc.sendPost(Const.Host + Const.Search, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				AppListBean parser = new AppListBean(new JSONObject(message));
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 搜索热词
	public List<String> searchHot() {
		StarHttpClient hpc = new StarHttpClient();
		List<String> hots = new ArrayList<String>();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.sendPost(Const.Host + Const.SearchHot,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				JSONArray array = new JSONArray(message);
				for (int i = 0; i < array.length(); i++) {
					hots.add(array.getJSONObject(i).getString("hotword"));
				}
				return hots;
			} else {
				return hots;
			}
		} catch (IOException e) {
			return hots;
		} catch (Exception e) {
			return hots;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 反馈
	public boolean reback(String content, String userKey) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("content", content);
				hpc.addParam("userkey", userKey);
				hpc.sendPost(Const.Host + Const.Advice, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				System.out.println("fankui = " + message);
				JSONObject object = new JSONObject(message);
				if ("success".equals(object.getString("result"))) {
					return true;
				}
			}
			return false;
		} catch (IOException e) {
			return false;
		} catch (Exception e) {
			return false;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 写评论
	public boolean writeComment(String userkey, String id, int star,
			String content) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("app_id", id);
				String dd = URLEncoder.encode(content, "utf-8");
				hpc.addParam("comment", dd);
				hpc.addParam("userkey", userkey);
				hpc.addParam("star", String.valueOf(star));
				hpc.sendPost(Const.Host + Const.Comment, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				boolean b = false;
				try {
					JSONObject j = new JSONObject(message);
					b = j.optString("result").equals("success");
				} catch (Exception e) {
					e.printStackTrace();
				}
				return b;
			} else {
				return false;
			}
		} catch (IOException e) {
			return false;
		} catch (Exception e) {
			return false;
		} finally {
			hpc.dispose();
		}
	}*/

	/*// 获取评论
	public CommentBeans getComments(String appId, String type, String page,
			String row) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("app_id", appId);
				hpc.addParam("type", type);
				hpc.addParam("page", page);
				hpc.addParam("rows", row);
				hpc.sendPost(Const.Host + Const.GetComment,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				CommentBeans parser = new CommentBeans(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

	/*public VertionBean getVertion() {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.addParam("platform", "android");
				hpc.addParam("channel", YangSession.getYangSession(null)
						.getSource());
				hpc.addParam("versionNum", UserPreference.read("versionNum", "1"));
				hpc.sendPost(Const.Host + Const.Vertion, Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				VertionBean parser = new VertionBean(message);
				return parser;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
		return null;
	}*/

	/*// 更新资料
	public boolean UpdateUserMessage(String userkey, String nickname, String sex) {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				String dd = URLEncoder.encode(nickname);
				hpc.addParam("nickname", dd);
				hpc.addParam("sex", sex);
				hpc.addParam("userkey", userkey);
				hpc.sendPost(Const.Host + Const.UpdateAccount,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				boolean b = false;
				try {
					JSONObject j = new JSONObject(message);
					b = j.optString("result").equals("success");
				} catch (Exception e) {
					e.printStackTrace();
				}
				return b;
			} else {
				return false;
			}
		} catch (IOException e) {
			return false;
		} catch (Exception e) {
			return false;
		} finally {
			hpc.dispose();
		}
	}*/

	// 获取可升级应用
//	public AppListBean getUpgrade(String data) {
//		StarHttpClient hpc = new StarHttpClient();
//		try {
//			if (Utils.hasNetwork(this.context)) {
//				hpc.addParam("data", data);
//				System.out.println("data = " + data.trim());
//				hpc.sendPost(Const.Host + Const.GetUpdateAppList,
//						Utils.isCMWAP(context));
//				String message = hpc.getReciveData();
//				AppListBean parser = new AppListBean(new JSONObject(message));
//				return parser;
//			} else {
//				return null;
//			}
//		} catch (IOException e) {
//			return null;
//		} catch (Exception e) {
//			return null;
//		} finally {
//			hpc.dispose();
//		}
//	}

	// 获取搜索热词
	public List<String> getSearchHot() {
		StarHttpClient hpc = new StarHttpClient();
		List<String> hots = new ArrayList<String>();
		try {
			if (Utils.hasNetwork(this.context)) {
				hpc.sendPost(Const.Host + Const.GetSearchHot,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				JSONArray array = new JSONArray(message);
				for (int i = 0; i < array.length(); i++) {
					hots.add(array.getJSONObject(i).getString("word"));
				}
				return hots;
			} else {
				return hots;
			}
		} catch (IOException e) {
			return hots;
		} catch (Exception e) {
			return hots;
		} finally {
			hpc.dispose();
		}
	}

	/*// get push message
	public PushMessage getPushMessage() {
		StarHttpClient hpc = new StarHttpClient();
		try {
			if (Utils.hasNetwork(this.context)) {
				UserPreference.ensureIntializePreference(context);
				String userkey = UserPreference.read("userkey", "");
				hpc.addParam("userkey", userkey);
				hpc.sendPost(Const.Host + Const.GetpushMessage,
						Utils.isCMWAP(context));
				String message = hpc.getReciveData();
				PushMessage parser = new PushMessage(message);
				return parser;
			} else {
				return null;
			}
		} catch (IOException e) {
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			hpc.dispose();
		}
	}*/

}
